//
//  CS_Page_DescriptionView.m
//  Pods
//
//  Created by 网新中研 on 2017/5/16.
//
//

#import "CS_Page_DescriptionView.h"

@interface CS_Page_DescriptionView()

@property (nonatomic, strong) UILabel *mainLabel;

@property (nonatomic, strong) UILabel *contentLabel;

@property (nonatomic, assign) BOOL isInformLabel;

@end

@implementation CS_Page_DescriptionView

-(NSInteger)viewType{
    return CSViewType_PageDescriptionView;
}

+(instancetype)descriptionViewWithTitle:(NSString *)title content:(NSString *)content{
    CS_Page_DescriptionView *descriptionView = [CS_Page_DescriptionView new];
    descriptionView.mainLabel.text = title;
    descriptionView.contentLabel.text = content;
    return descriptionView;
}

/**
 * 提示性用于
 */
+(instancetype)informWithcontent:(NSString *)content{
    CS_Page_DescriptionView *descriptionView = [CS_Page_DescriptionView new];
    descriptionView.contentLabel.text = content;
    descriptionView.isInformLabel = YES;
    return descriptionView;
}

/**
 *  父视图
 *
 *  @param superView
 *
 *  @return 添加该视图后的Y坐标高度
 */
-(CGFloat)layoutView:(UIView *)superView primaryY:(CGFloat)primaryY{
    
    primaryY += self.moreTopSpace;
    CGFloat originY = 0;
    
    if (self.textAlignment) {
        self.mainLabel.textAlignment = self.textAlignment;
        self.contentLabel.textAlignment = self.textAlignment;
    }
    
    if (![self isTitleEmpty]) {
        self.mainLabel.frame = CGRectMake(CELL_EDIT_RIGHT_MARGIN, 0, kWindowW - 2 * CELL_EDIT_RIGHT_MARGIN, 30);
        [self addSubview:self.mainLabel];
        originY += 30;
    }else if(self.isInformLabel){
        _contentLabel.font = [UIFont systemFontOfSize:13.0];
        _contentLabel.textColor = [UIColor colorFromHexCode:@"8b6420"];
    }else{
        self.contentLabel.font = [UIFont systemFontOfSize:13.0];
    }
    
    if (![self isContentEmpty]) {
        
        NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc]initWithString:[NSString stringWithFormat:@"%@", self.contentLabel.text]];
        NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc]init];
        [paragraphStyle setLineSpacing:6.0];
        [paragraphStyle setAlignment:self.textAlignment];
        [attributedString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, self.contentLabel.text.length)];
        self.contentLabel.attributedText = attributedString;
        
        
        self.contentLabel.frame = CGRectMake(CELL_EDIT_RIGHT_MARGIN, originY, kWindowW - 2 * CELL_EDIT_RIGHT_MARGIN, 0);
        [self.contentLabel sizeToFit];
        self.contentLabel.frame = CGRectMake(CELL_EDIT_RIGHT_MARGIN, originY, kWindowW - 2 * CELL_EDIT_RIGHT_MARGIN, self.contentLabel.frame.size.height);
        
        if (!self.isInformLabel) {
            self.contentLabel.frame = CGRectMake(CELL_EDIT_RIGHT_MARGIN, originY, kWindowW - 2 * CELL_EDIT_RIGHT_MARGIN, 0);
            [self.contentLabel sizeToFit];
            self.contentLabel.frame = CGRectMake(CELL_EDIT_RIGHT_MARGIN, originY, kWindowW - 2 * CELL_EDIT_RIGHT_MARGIN, self.contentLabel.frame.size.height);
            [self addSubview:self.contentLabel];
            originY += self.contentLabel.frame.size.height;
            
        }else{
            self.contentLabel.frame = CGRectMake(10, 10, kWindowW - 2 * CELL_EDIT_RIGHT_MARGIN - 2 * 10, 0);
            [self.contentLabel sizeToFit];
            self.contentLabel.frame = CGRectMake(10, 10, kWindowW - 2 * CELL_EDIT_RIGHT_MARGIN  - 2 * 10, self.contentLabel.frame.size.height);
            
            UIView *backgroundView = [UIView new];
            backgroundView.backgroundColor = [UIColor colorFromHexCode:@"ffe5ab"];
            backgroundView.frame = CGRectMake(CELL_EDIT_RIGHT_MARGIN, originY, kWindowW - 2 * CELL_EDIT_RIGHT_MARGIN, self.contentLabel.frame.size.height + 20);
            [backgroundView addSubview:self.contentLabel];
            [self addSubview:backgroundView];
            originY += self.contentLabel.frame.size.height + 20;
        }
        
        if (![self isTitleEmpty]) {
            originY += 15;
        }
        
    }
    
    self.frame = CGRectMake(0, primaryY, kWindowW, originY);
    
    self.backgroundColor = [UIColor clearColor];
    [superView addSubview:self];
    return primaryY + originY + self.moreBottomSpace;
}

-(BOOL)isTitleEmpty{
    return (self.mainLabel.text == nil
            ||[self.mainLabel.text isEqualToString:@""]);
}

-(BOOL)isContentEmpty{
    return (self.contentLabel.text == nil
             ||[self.contentLabel.text isEqualToString:@""]);
}

-(UILabel *)mainLabel{
    if (_mainLabel == nil) {
        _mainLabel = [UILabel new];
        _mainLabel.font = [UIFont systemFontOfSize:18.0];
        _mainLabel.textColor = [UIColor colorFromHexCode:@"1e1e1e"];
        _mainLabel.textAlignment = NSTextAlignmentLeft;
        _mainLabel.numberOfLines = 0;
    }
    return _mainLabel;
}

-(UILabel *)contentLabel{
    if (_contentLabel == nil) {
        _contentLabel = [UILabel new];
        _contentLabel.font = [UIFont systemFontOfSize:15.0];
        _contentLabel.textColor = COLOR_TEXT_3;
        _contentLabel.numberOfLines = 0;
        
        
    }
    return _contentLabel;
}

@end
